username and password at https://twitter.com/.username and passwordCreate a new AppCreate a Twitter app at https://apps.twitter.com/.
Fill in with you App name, brief description and a website. Ideally this website is where people can find more information about your app. However, if you don’t have one just insert a valid link to a web page.
As callback URL set: http://127.0.0.1:1410
Setup your app information andcallback URL.
Create your Twitter Application you will be re-directed to your application page. You’re almost there! Go to the Keys and Access Tokens tab and at the bootom of this page click on Create my access token. Your access token will appear at the bottom of the page.Create access token to connect R with Twitter.
R with Twitter! From your application page you need four things:Create access token to connect R with Twitter.
There are two well-known packages used to collect data from Twitter directly into R:
twitteR packageThe function twitteR::searchTwitter will return a list of tweets which you can easily coherce to standard data.frame with the function twitteR::twListToDF.
> api_key <- "your_api_key"
> api_secret <- "your_api_secret"
> token <- "your_token"
> token_secret <- "your_token_secret"
>
> setup_twitter_oauth(api_key, api_secret, token, token_secret)
>
> # Search tweets
> MyTweets <- searchTwitter("#MyChosenHastags", lang = "en")
rtweet packageNote: Remember to set set_renv=FALSE when you run the rtweet::create_token() function. The default should be set to FALSE as mentioned in the help page ?rtweet but it is instead set to TRUE. If set_renv=TRUE it will save a hidden token named .rtweet_token.rds which will be used as the default evironemnt twietter token variable.
More information about this can be found at Obtaining and using access tokens.
The function rtweet::search_tweets returns tweets already as a data frame.
> appname <- "Rladies_app"
> api_key <- "your_api_key"
> api_secret <- "your_api_secret"
>
> setup_twitter_oauth(api_key, api_secret, token, token_secret)
>
> ## create token named 'twitter_token'
> twitter_token <- create_token(app = appname, consumer_key = key, consumer_secret = secret)
>
> MyTweet <- search_tweets("#MyChosenHastags", lang = "en", token = twitter_token)
If you do not have a Google account, you can create one at Google
Go to the [Google Developers Console] (https://console.developers.google.com/) and create a project there
Obtain authorisation credentials
To use YouTube data API you need to have authorization credentials. The API supports API keys and OAuth 2.0 credentials.
The tuber package uses OAuth 2.0, but you will also need the API keys for some data extraction, e.g. obtaining YouTube channel ID.
To set up your credentials, you go to the Credentials section in your Google Developers Console.
For the OAuth 2.0 setup, set “Authorized redirect URIs” as http://localhost:1410/
Note down the following:
Your API key:OAuth 2.0 credentials:Name:Client ID:Client secret:> install.packages("devtools")
> install_github("pablobarbera/Rfacebook", subdir = "Rfacebook")
> install.packages("httr")
> library(devtools)
> library(Rfacebook)
> library(httr)
> library(jsonlite)
> app_id1 <- "useYours"
> app_secret1 <- "useYous"
>
> fb_oauth <- fbOAuth(app_id = app_id1, app_secret = app_secret1, extended_permissions = FALSE)
> facebook <- oauth_endpoint(authorize = "https://www.facebook.com/dialog/oauth",
+ access = "https://graph.facebook.com/oauth/access_token")
Credentials you get from registering a new application
> app_name <- "useR2018" #not used but good for organisation
> client_id = app_id1
> client_secret = app_secret1
>
> myapp <- oauth_app(app_name, client_id, client_secret)
>
>
> facebook_oauth <- oauth2.0_token(facebook, myapp)
> save(facebook_oauth, file = "fb_oauth")
>
> load("fb_oauth")
>
> token <- facebook_oauth$credentials$access_token
>
> me <- getUsers("me", token = fb_oauth)
>
> me2 <- getUsers("me", token = token)
>
> # getUsers(c('barackobama', 'donaldtrump'), token) getUsers(c('barackobama',
> # 'donaldtrump'), fb_oauth)
> install.packages("httpuv")
> install.packages("httr")
> install.packages("jsonlite")
> install.packages("RCurl")
> require(httr)
> require(jsonlite)
> require(RCurl)
> full_url <- oauth_callback()
> full_url <- gsub("(.*localhost:[0-9]{1,5}/).*", x = full_url, replacement = "\\1")
> print(full_url)
>
> # http://localhost:1410/
>
> # register your client in Instagram Parameters to use
>
> # define 4 variables get the first 3 from the instagram setup
> app_name <- "useYour"
> client_id <- "useYour"
> client_secret <- "useYour"
>
> # set the type of access
> scope = "public_content"
>
> # set access points for authorization
> # https://www.instagram.com/developer/endpoints/users/
> instagram <- oauth_endpoint(authorize = "https://api.instagram.com/oauth/authorize",
+ access = "https://api.instagram.com/oauth/access_token")
>
> # the application that will be used to access Ig
> myapp <- oauth_app(app_name, client_id, client_secret)
>
> # authentication
>
> # ig_oauth <- oauth2.0_token(instagram, myapp,scope='basic', type =
> # 'application/x-www-form-urlencoded',cache=FALSE)
>
> ig_oauth <- oauth2.0_token(instagram, myapp, scope = "basic")
>
>
> save(ig_oauth, file = "ig_oauth")
> load("ig_oauth")
>
> token <- ig_oauth$credentials$access_token